package com.example.weatherforecast.city_manager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weatherforecast.R;
import com.example.weatherforecast.bean.WeatherBean;
import com.example.weatherforecast.database.DatabaseBean;
import com.google.gson.Gson;

import java.util.List;

public class CityManagerAdapter extends RecyclerView.Adapter<CityManagerAdapter.MyViewHolder> {
    Context context;
    // 显示的城市数据源
    private List<DatabaseBean> list;

    // 点击和长按事件的接口
    private OnItemClickListener mItemClickListener;
    private OnLongClickListener mLongClickListener;

    public CityManagerAdapter(Context context, List<DatabaseBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public void setLongClickListener(OnLongClickListener longClickListener) {
        mLongClickListener = longClickListener;
    }

    @NonNull
    @Override
    //加载item布局文件，并返回MyViewHolder对象
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_city_manager, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    //操作item，给控件填入数据
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        DatabaseBean bean = list.get(position);
        holder.cityTv.setText(bean.getCity());
        WeatherBean weatherBean = new Gson().fromJson(bean.getContent(), WeatherBean.class);
        //获取今日天气情况
        WeatherBean.ResultsBean.WeatherDataBean dataBean = weatherBean.getResults().get(0).getWeather_data().get(0);
        holder.conTv.setText("天气:" + dataBean.getWeather());
        String[] split = dataBean.getDate().split("：");
        String todayTemp = split[1].replace(")", "");
        holder.currentTempTv.setText(todayTemp);
        holder.windTv.setText(dataBean.getWind());
        holder.tempRangeTv.setText(dataBean.getTemperature());
        // holder.tvItemContent.setText("item------------" + position);
        //设置点击和长按事件
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mItemClickListener.onItemClick(position);
                }
            });
        }
        if (mLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    return mLongClickListener.onLongClick(position);
                }
            });
        }
    }

    @Override
    //获取列表条目总数
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public interface OnLongClickListener {
        boolean onLongClick(int position);
    }

    //缓存视图对象
    class MyViewHolder extends RecyclerView.ViewHolder {
        //视图里存在的控件
        TextView cityTv, conTv, currentTempTv, windTv, tempRangeTv;

        public MyViewHolder(View view) {
            super(view);
            cityTv = itemView.findViewById(R.id.itemCityTvCity);
            conTv = itemView.findViewById(R.id.itemCityTvCondition);
            currentTempTv = itemView.findViewById(R.id.itemCityTvTemperature);
            windTv = itemView.findViewById(R.id.itemCityTvWind);
            tempRangeTv = itemView.findViewById(R.id.itemCityTvTemperatureRange);
        }

    }
}
